package com.wgtgt.code_ploymerization;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;

/**
 * Hello world!
 *
 */
public class App {
	public static final String exts = ".java";

	public static void main(String[] args) {
		System.out.println("Hello World!");
		String basePath = "F:\\temp\\ice-notepad";
		File baseFile = new File(basePath);
		List<String> list = new ArrayList<>();
		scan(baseFile, list);
		basePath = basePath.replace("\\", "/");
		String left = StrUtil.subBefore(basePath, "/", true);
		String right = StrUtil.subAfter(basePath, "/", true);
		String newPath = left + "/" + right + "_out.txt";
		String content = joinText(list);
		FileUtil.writeUtf8String(content, newPath);
	}

	private static String joinText(List<String> list) {
		StringBuilder sb = new StringBuilder();
		if (list != null) {
			for (String line : list) {
				if (StrUtil.isNotBlank(line)) {
					sb.append(line);
					sb.append("\r\n");
				}
			}
		}
		return sb.toString();
	}

	private static void scan(File file, List<String> list) {
		if (file.isFile()) {
			List<String> splitTrim = StrUtil.splitTrim(exts, ',');
			String name = file.getName();
			String ext = StrUtil.subAfter(name, ".", true);
			if (splitTrim.contains("." + ext)) {
				System.out.println(file.getAbsolutePath());
				List<String> readUtf8Lines = FileUtil.readUtf8Lines(file);
//				readUtf8Lines.addAll(readUtf8Lines);
				if (readUtf8Lines != null) {
					for (String line : readUtf8Lines) {
						if (StrUtil.isNotBlank(line)) {
							list.add(line);
						}
					}
				}
			}
		} else if (file.isDirectory()) {
			File[] listFiles = file.listFiles();
			if (listFiles != null) {
				for (File subFile : listFiles) {
					scan(subFile, list);
				}
			}
		}

	}
}
